package com.leyou.auth.service;

import com.leyou.auth.client.UserClient;
import com.leyou.auth.config.JwtProperties;
import com.leyou.auth.pojo.UserInfo;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.user.pojo.User;
import com.sun.org.apache.bcel.internal.generic.FNEG;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthService {

    @Autowired
    private UserClient userClient;



    //根据用户名密码授权，首先要验证用户名密码的正确性
    public String accredit(String username, String password, JwtProperties jwtProp) {

        User user = this.userClient.queryUser(username, password);

        if (user != null) {
            UserInfo userInfo = new UserInfo();

            BeanUtils.copyProperties(user,userInfo);

            try {
                return JwtUtils.generateToken(userInfo,jwtProp.getPrivateKey(),jwtProp.getExpire());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
